package com.douqu.game.battle.controller;

import com.douqu.game.battle.controller.sprite.PlayerController;
import com.douqu.game.battle.controller.sprite.SpriteController;
import com.douqu.game.battle.entity.tmp.MonsterBattleTmp;
import com.douqu.game.battle.entity.tmp.SpriteBattleTmp;
import com.douqu.game.battle.util.BattleUtils;
import com.douqu.game.core.astar.AStar;
import com.douqu.game.core.astar.AstarMap;
import com.douqu.game.core.config.activity.CampRobotConfig;
import com.douqu.game.core.config.battle.StarCheckConfig;
import com.douqu.game.core.config.challenge.LevelConfig;
import com.douqu.game.core.config.challenge.MapConfig;
import com.douqu.game.core.e.E_BattleDetailType;
import com.douqu.game.core.entity.activity.MatchBaseInfo;
import com.douqu.game.core.entity.battle.BattleInitParam;
import com.douqu.game.core.entity.db.CardDB;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.CoreUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-27 17:01
 */
public class PVEController extends BattleController
{

    public PVEController(String battleId, PlayerController player, SpriteController target, SGCommonProto.E_BATTLE_TYPE battleType, E_BattleDetailType battleDetailType, String targetData, String targetName, BattleInitParam initParam)
    {
        super(battleId, player, target, battleType, battleDetailType, targetData, targetName, initParam);

        this.targetData = targetData;

        if(E_BattleDetailType.PVE_INSTANCE == battleDetailType)
        {
            String[] values = targetData.split(ConstantFactory.UNDERLINE);
            player.setSupportCardId(Integer.parseInt(values[1]));
            player.setSupportCardParamId(Integer.parseInt(values[2]));
        }

        MapConfig mapConfig = DataFactory.getInstance().getGameObject(DataFactory.MAP_KEY, initParam.getMapId());
        LogUtils.debug("mapConfig -> " + mapConfig);
        this.astar = new AStar(new AstarMap(AstarMap.AnchorPos.BottomLeft, mapConfig.mapType==1? BattleUtils.BATTLE_MAP_1:BattleUtils.BATTLE_MAP_2));
    }

    @Override
    public void init()
    {
        super.init();

        SpriteBattleTmp pbt2 = new MonsterBattleTmp();
        pbt2.setReady(true);
        pbt2.init(this, target, ConstantFactory.BATTLE_TEAM_2);
        target.setAttachment(pbt2);

        pbt2.setTargetBattleTmp(source.getAttachment());
        source.getAttachment().setTargetBattleTmp(pbt2);

        battleDetail.getTeam2Info().setMaster(pbt2.getMasterSoldier().getSpriteTemp());

        if(E_BattleDetailType.PVE_SPAN_CAMP == battleDetailType)
        {
            CampRobotConfig robotConfig = DataFactory.getInstance().getGameObject(DataFactory.CAMP_ROBOT_KEY, Integer.parseInt(targetData));
            MatchBaseInfo baseInfo = new MatchBaseInfo(robotConfig, 0);
            baseInfo.setServerId(target.getServerId());
            if(!CoreUtils.isNullOrEmpty(targetName))
                baseInfo.setName(targetName);
            battleDetail.getTeam2Info().setBaseInfo(baseInfo);
        }

        if(!battleDetailType.isPlayerBattle())
        {
            readyStart();
        }
    }

    @Override
    public int getStar(SpriteBattleTmp spriteBattleTmp, int winTeam)
    {
        if(spriteBattleTmp == null || winTeam <= 0)
            return 0;

        if(winTeam != spriteBattleTmp.getTeamNo())
            return 0;

        if(E_BattleDetailType.PVE_INSTANCE == battleDetailType)
        {
            DataFactory dataFactory = DataFactory.getInstance();
            StarCheckConfig starCheckConfig = null;
            LevelConfig levelConfig = dataFactory.getGameObject(DataFactory.LEVEL_KEY, Integer.parseInt(targetData.split(ConstantFactory.UNDERLINE)[0]));
            int[] starResult = new int[levelConfig.starChecks.length];
            for(int i = 0; i < levelConfig.starChecks.length; i++)
            {
                starCheckConfig = dataFactory.getGameObject(DataFactory.STAR_CHECK_KEY, levelConfig.starChecks[i]);
                if(starCheckConfig.type == ConstantFactory.STAR_TYPE_WIN)
                {
                    if(winTeam == spriteBattleTmp.getTeamNo())
                        starResult[i] = 1;
                }
                else if(starCheckConfig.type == ConstantFactory.STAR_TYPE_BATTLE_TIME)
                {
                    int time = battleTime / 1000;
                    if(time >= starCheckConfig.values[0] && time <= starCheckConfig.values[1])
                        starResult[i] = 1;
                }
                else if(starCheckConfig.type == ConstantFactory.STAR_TYPE_HP_RATE)
                {
                    long hp = spriteBattleTmp.getMasterSoldier().getHp();
                    long maxHp = spriteBattleTmp.getMasterSoldier().getMaxHp();
                    float temp = (float) hp / maxHp;
                    int rate = (int) (temp * 100);
                    if(rate >= starCheckConfig.values[0] && rate <= starCheckConfig.values[1])
                        starResult[i] = 1;
                }
                else if(starCheckConfig.type == ConstantFactory.STAR_TYPE_CAMP_ID)
                {
                    for(CardDB cardDB : spriteBattleTmp.getHistoryCards())
                    {
                        if(starCheckConfig.values[0] == cardDB.getConfig().getSoldier().camp)
                        {
                            starResult[i] = 1;
                            break;
                        }
                    }
                }
                else if(starCheckConfig.type == ConstantFactory.STAR_TYPE_KILL_COUNT)
                {
                    if(spriteBattleTmp.killCount >= starCheckConfig.values[0])
                        starResult[i] = 1;
                }
                else if(starCheckConfig.type == ConstantFactory.STAR_TYPE_CAMP_COUNT)
                {
                    int count = 0;
                    for(CardDB cardDB : spriteBattleTmp.getHistoryCards())
                    {
                        if(starCheckConfig.values[0] == cardDB.getConfig().getSoldier().camp)
                            count++;
                    }
                    if(count >= starCheckConfig.values[1])
                        starResult[i] = 1;
                }
            }

            StringBuilder result = new StringBuilder();
            for(int r : starResult)
                result.append(r);

            return Integer.valueOf(result.toString(), 2);//把二进制的结果转换成十进制
        }
        else
        {
            return 0;
        }

    }
}
